package com.lbn.jfqsc.app.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lbn.baseframe.base.BaseFragment;
import com.lbn.baseframe.base.BaseRecyclerAdapter;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.home.adapter.HomeItemAdapter;
import com.lbn.jfqsc.app.home.detail.DetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeFragment extends BaseFragment<HomePresenter> implements IHomeView,XRecyclerView.LoadingListener,BaseRecyclerAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    private HomeItemAdapter adapter;
    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(rootView==null){
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
            ButterKnife.bind(this, rootView);
        }
        return rootView;
    }

    @Override
    public HomePresenter createPresenter() {
        return new HomePresenter();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter = new HomeItemAdapter(getContext(),null);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        recyclerView.setRefreshing(true);
    }
    int page = 1;
    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.goods(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.goods(page);
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        int id = adapter.getItem(pos-1).getGoodsId();
        DetailActivity.startActivity(getContext(),id);
    }

    @Override
    public void isMore(boolean b) {
        recyclerView.setNoMore(!b);
    }

    @Override
    public void setData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    @Override
    public void addData(List list) {
        adapter.addAll(list);
        recyclerView.loadMoreComplete();
    }
}
